package com.msmall.community.klg.preparer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.klg.model.Article;

public class HotKlgPreparer implements ViewPreparer{
	private static final Log log = LogFactory.getLog(HotKlgPreparer.class);
	
	@SuppressWarnings("unchecked")
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Map<Long, List<Article>> klgSubSegmentHot = (Map<Long, List<Article>>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_SUBSEGMENT_HOT);
			List<Article> articles = new ArrayList<Article>();
			for(List<Article> vs : klgSubSegmentHot.values()){
				if(vs != null){
					if(vs.size() > 2){
						articles.addAll(vs.subList(0, 2));
					}else{
						articles.addAll(vs);
					}
				}
			}
			request.setAttribute("hotKlgArticles", articles);
		}catch(Exception e){
			log.debug(e);
		}
	}
}